<?php
use Doctrine\ORM\EntityRepository;
use DoctrineExtensions\Paginate\Paginate;

class ArticleRepository
	extends EntityRepository
{
	public function getArticles($i_offSet = 0, $i_maxResults = 10)
	{
		$dql = 'SELECT a FROM Article a JOIN a.user b WHERE a.user = b.userId ORDER BY a.createDate asc';
		//$dql = 'SELECT a FROM Article a ORDER BY a.createDate DESC';
		$rs = $this->getEntityManager()->createQuery($dql);
		$rs->setMaxResults(10);
		//$rs->setFirstResult($i_offSet)->setMaxResults($i_maxResults)->getResult(); // Step 2
		return $rs->getResult();
		
		
	}
	
	public function getArticlesByPage($i_offSet = 0, $i_maxResults = 10, $i_order=2, $s_dir = 'asc', $i_sEcho)
	{
		//$dql = 'SELECT a FROM Article a JOIN a.user b WHERE a.user = b.userId';
		$arr_columns = array('b.userName', 'a.articleTitle', 'a.createDate');
		
		$o_qb = $this->getEntityManager()->createQueryBuilder();
		$o_qb->select('a');
		$o_qb->from('Article', 'a');
		$o_qb->join('a.user', 'b', 'WITH' , $o_qb->expr()->eq('a.user', 'b.userId'));
		/*
		if($s_searchString != ''){
			$o_qb->orWhere($o_qb->expr()->like($arr_columns[0], $o_qb->expr()->literal('%'.$s_searchString.'%')));
			$o_qb->orWhere($o_qb->expr()->like($arr_columns[1], $o_qb->expr()->literal('%'.$s_searchString.'%')));
			$o_qb->orWhere($o_qb->expr()->like($arr_columns[2], $o_qb->expr()->literal('%'.$s_searchString.'%')));
		}
		*/
		if($i_order !=3){
			$o_qb->orderBy($arr_columns[$i_order], $s_dir);
		}
		
		
		$dql = $o_qb->getDQL();
		try{
			//set the offsett and the limit
			$rs = $this->getEntityManager()->createQuery($dql);
			$rs->setFirstResult($i_offSet)->setMaxResults($i_maxResults)->getResult(); // Step 2
		}catch(Exception $e){
			die($e);
		}
		$res = $rs->getResult();
		
		$arr_page = array(
			"sEcho"=> $i_sEcho,
			"iTotalRecords"=> $this->getArticleCount(),
		 	"iTotalDisplayRecords"=> $this->getArticleCount(),
		 	"aaData" => $this->convertToPaginationFormat($res)
		);
		
		return $arr_page;
	}
	
	public function getArticleCount()
	{
		$dql = 'SELECT a FROM Article a JOIN a.user b WHERE a.user = b.userId ORDER BY a.createDate asc';
		$rs = $this->getEntityManager()->createQuery($dql);
		$count = Paginate::getTotalQueryResults($rs); // Step 1
		
		return $count;
	}
	
	private function convertToPaginationFormat($arr_o_articles)
	{
		$arr_rows = array();
		if(!empty($arr_o_articles)){
			$arr_updateArticleAppConf = Claycode_configuration::getInstance()->getAppConfArray('updateArticleAlias');
			$arr_deleteArticleAppConf = Claycode_configuration::getInstance()->getAppConfArray('deleteArticleTest');
				
			foreach($arr_o_articles as $o_writtenArticle){
				$s_articleId = EncryptionHandler::get()->doEncrypt($o_writtenArticle->getId());
				$s_changeArticleUrl = fw_util_buildUrl($arr_updateArticleAppConf['controller_class_alias'], '', true, false);
				$s_changeArticleUrl .= '/' . 'articleId' . '/' . $s_articleId;
				$s_deleteArticleUrl = fw_util_buildUrl($arr_deleteArticleAppConf['controller_class_alias'], '',  true, false);
				$s_deleteArticleUrl .= '/' . 'articleId' . '/' . $s_articleId;
	
				$arr_rows[] = array(
					$o_writtenArticle->getWriter()->getUserName(),
					'<a href="'. $s_changeArticleUrl .'">' . $o_writtenArticle->getArticleTitle() . '</a>',
					$o_writtenArticle->getCreateDate()->format('M d, Y H:m:s'),
					'<a href="'. $s_deleteArticleUrl .'">remove</a>'
				);
			}
		}
		return $arr_rows;
	}
}